<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>Document</title>
        <script src="../vue.js"></script>
    </head>
    <body>
        <div id="app">
            <input type="text" v-model="name" />
            <p>姓名: <input type="text" v-model="formData.name" /></p>
            <p>年龄: <input type="text" v-model="formData.age" /></p>
        </div>
        <script>
            // watch不推荐使用箭头函数
            let vm = new Vue({
                el: '#app',
                data() {
                    return {
                        name: '',
                        formData: {
                            name: '',
                            age: '',
                        },
                    };
                },
                methods: {
                    watchName(newVal, oldVal) {
                        console.log(newVal, 'watchName');
                        // console.log(oldVal);
                    },
                },
                watch: {
                    // 是下面name的简写
                    // name(newVal, oldVal) {
                    //     console.log(newVal);
                    // },
                    // name: 'watchName',
                    // name: {
                    //     handler(newVal, oldVal) {
                    //         console.log(newVal);
                    //     },
                    //     // deep: true,
                    // },
                    formData: {
                        handler(newVal, oldVal) {
                            // console.log(newVal);
                        },
                        deep: true,
                    },
                    'formData.name': {
                        handler(newVal, oldVal) {
                            console.log(newVal);
                        },
                    },
                    'formData.name'(newVal, oldVal) {
                        console.log(newVal);
                    },
                    name: [
                        'watchName',
                        function watchName1(newVal, oldVal) {
                            console.log(newVal, 'watchName1');
                            // console.log(oldVal);
                        },
                        {
                            handler(newVal, oldVal) {
                                console.log(newVal, 'watchName2');
                                // console.log(oldVal);
                            },
                        },
                    ], 
                },
            });
        </script>
    </body>
</html>
